﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demos.DesignPatterns._07_适配器模式
{

    /// <summary>
    /// Paypal支付适配器
    /// </summary>
    public class PayPalAdapter: IPaymentGateway
    {
        private readonly PaypalService _paypal;

        public PayPalAdapter(PaypalService paypal)
        {
            _paypal = paypal;
        }

        /// <summary>
        /// 获取支付状态
        /// </summary>
        /// <param name="transactionId"></param>
        /// <returns></returns>
        public string GetPaymentStatus(string transactionId)
        {
            return _paypal.GetPayPalPaymentStatus(transactionId);
        }

        /// <summary>
        /// 处理支付
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="amount"></param>
        /// <param name="currency"></param>
        /// <returns></returns>
        public bool ProcessPayment(string businessId, decimal amount, string currency)
        {
            // 处理货币转换（假设汇率1:0.14）
            if (currency != "USD")
                amount = ConvertCurrency(amount, currency, "USD");

            return _paypal.MakePayment(businessId, amount);
        }

        private decimal ConvertCurrency(decimal amount, string fromCurrency, string toCurrency)
        {
            // 简化货币转换逻辑
            if (fromCurrency == "CNY" && toCurrency == "USD")
                return amount * 0.14m;
            return amount;
        }
    }
}
